package com.hubu.jwxt.service;

import com.hubu.jwxt.beans.Student;
import com.hubu.jwxt.beans.Teacher;
import com.hubu.jwxt.dao.StudentDAO;
import com.hubu.jwxt.dao.TeacherDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginService {

    @Autowired
    private TeacherDAO teacherDAO;
    @Autowired
    private StudentDAO studentDAO;

    public Map check(String num,String pwd,String role){
        Map<String, Object> rs = new HashMap<>();

        if ("teacher".equals(role)){
            Teacher teacher = teacherDAO.selectByNum(num);

            if (teacher!=null && teacher.getT_pwd().equals(pwd)){
                rs.put("msg","教师登陆成功");
                rs.put("user",teacher);
            }else {
                rs.put("msg","教师登陆失败");
                rs.put("user",null);
            }
        }
        else if("student".equals(role)){
            Student student = studentDAO.selectByNum(num);

            if (student!=null && student.getS_pwd().equals(pwd)){
                rs.put("msg","学生登陆成功");
                rs.put("user",student);
            }else {
                rs.put("msg","学生登陆失败");
                rs.put("user",null);
            }
        }
        else{
            rs.put("msg","角色信息不正确");
            rs.put("user",null);
        }
        return rs;
    }
}
